# -*- coding: utf-8 -*-

import gitlab


class GitlabAPI(object):
    def __init__(self, *args, **kwargs):
        self.gl = gitlab.Gitlab('http://git.xxx.club',
                                private_token='34bfgsou_cMzxAKZp6Uf')
        self.projects = None
        self.branch = "master"

    def get_user_id(self, username):
        """
        通过用户名获取用户id
        :param username:
        :return:
        """
        user = self.gl.users.get_by_username(username)
        return user.id

    def get_group_ids(self, groupname, web_url):
        """
        通过组名获取组id
        :param groupname:
        :return:
        """
        Id = self.gl.projects.list(search=groupname)
        ids = 0
        for k in Id:
            if k.http_url_to_repo == web_url:
                ids = k.id
                break
        return ids

    def get_group_id(self, groupname):
        """
        通过组名获取组id
        :param groupname:
        :return:
        """
        Id = self.gl.projects.list(search=groupname)
        return Id[0].id

    def get_group_id_git(self, git):
        """
        通过git获取组id
        :param groupname:
        :return:
        """
        id = self.gl.projects.list(all=True)
        ids = 0
        for _id in id:

            if _id.http_url_to_repo == git:

                ids = _id.id
                break
        return ids

    def get_user_projects(self, userid):
        """
        获取用户所拥有的项目
        :param userid:
        :return:
        """
        projects = self.gl.projects.owned(userid=userid, all=True)
        result_list = []
        for project in projects:
            result_list.append(project.http_url_to_repo)
        return result_list

    def get_group_projects(self, groupname):
        """
        获取组内项目！！！！！！！其他博客也有类似方法，实测不能拿到群组内项目，现经过小改动，亲测可满足要求
        :param groupname:
        :return:
        """
        group = self.gl.groups.get(groupname, all=True)
        projects = group.projects.list(all=True)
        return projects

    def getFile(self, file):
        f = self.projects.files.get(file_path=file, ref=self.branch)
        file_content = f.decode()
        file_content = file_content.decode()
        return file_content

    def getContent(self, projectID, path=None):
        """
        通过项目id获取文件内容
        :param projectID:
        :return:
        """
        self.projects = self.gl.projects.get(projectID)
        fileList = self.projects.repository_tree(
            all=True, path=path, ref=self.branch)
        l = []
        for i in fileList:
            print(i)

            s = i["path"].split("/")
            l.append(s[-1])
        return l

    def getContentWeb(self, projectID, branch, path=None):
        """
        通过项目id获取文件内容
        :param projectID:
        :return:
        """
        self.projects = self.gl.projects.get(projectID)
        fileList = self.projects.repository_tree(
            all=True, path=path, ref=branch)
        l = []
        for i in fileList:
            print(i)

            s = i["path"].split("/")
            l.append(s[-1])
        return l

    def get_branch(self, projectID):
        l = []
        s = self.gl.projects.get(projectID)
        for b in s.branches.list():

            l.append(b.name)
        return l

    def get_all_group(self):
        """
        获取所有群组
        :return:
        """
        return self.gl.groups.list(all=True)


if "__main__" == __name__:

    # g=GitlabAPI()

    # fileList = self.projects.repository_tree(all=True,path=path, ref=self.branch)

    # g.getContent(222,path="")

    # Id=g.get_group_id_git("http://git.xx.xx/web/luodipage.git")
    # print (Id)
    # p=g.getContent(Id,"master")
    # print (len(p))
    # print

    # id = g.gl.projects.list(all=True)
    # git="http://git.xx.xx/gongbenkai/test.git"
    # for _id in id :

    #     if _id.http_url_to_repo==git:

    #          _id.id

    # print (id)
    # print projects
    # branch = p.branches.get('master')
    # print(p)
    # print branch
